/*
 *所有关于s类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*sService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using VOL.Core.Extensions;
using VOL.Core.ManageUser;
using VOL.Core.UserManager;
using VOL.Core.Utilities;
using VOL.Entity.DomainModels;
using VOL.Entity.ResDto;
using VOL.System.IRepositories;

namespace VOL.System.Services
{
    public partial class sys_departmentService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly Isys_departmentRepository _repository;//访问数据库

        [ActivatorUtilitiesConstructor]
        public sys_departmentService(
            Isys_departmentRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }

        public override PageGridData<sys_department> GetPageData(PageDataOptions options)
        {
            FilterData();
            return base.GetPageData(options);
        }

        private void FilterData()
        {
            //限制 只能看自己部门及下级组织的数据
            QueryRelativeExpression = (IQueryable<sys_department> queryable) =>
            {
                if (UserContext.Current.IsSuperAdmin)
                {
                    return queryable;
                }
                var deptIds = UserContext.Current.GetAllChildrenDeptIds();
                return queryable.Where(x => deptIds.Contains(x.DepartmentId));
            };
        }
        public override WebResponseContent Export(PageDataOptions pageData)
        {
            FilterData();
            return base.Export(pageData);
        }

        WebResponseContent webResponse = new WebResponseContent();
        public override WebResponseContent Add(SaveModel saveDataModel)
        {
            AddOnExecuting = (sys_department dept, object list) =>
            {
                return webResponse.OK();
            };
            return base.Add(saveDataModel).Reload();
        }
        public override WebResponseContent Update(SaveModel saveModel)
        {
            UpdateOnExecuting = (sys_department dept, object addList, object updateList, List<object> delKeys) =>
            {
                if (_repository.Exists(x => x.DepartmentId == dept.ParentId && x.DepartmentId == dept.DepartmentId))
                {
                    return webResponse.Error("上级组织不能选择自己");
                }
                if (_repository.Exists(x => x.ParentId == dept.DepartmentId) && _repository.Exists(x => x.DepartmentId == dept.ParentId))
                {
                    return webResponse.Error("不能选择此上级组织");
                }
                return webResponse.OK();
            };
            return base.Update(saveModel).Reload();
        }

        public override WebResponseContent Del(object[] keys, bool delList = true)
        {
            return base.Del(keys, delList).Reload();
        }

        public WebResponseContent GetRegion()
        {
            string sql = "  ";
            var regiondata = repository.DbContext.Query<sys_department>().ToList().OrderBy(item => item.DepartmentCode).ToList();
            List<RegionDto> data = new List<RegionDto>();
            foreach (var item in regiondata)
            {
                if (item.ParentId == null)
                {
                    RegionDto addRegion = new RegionDto { value = item.DepartmentCode + "|" + item.DepartmentName, label = item.DepartmentName };
                    data.Add(addRegion);
                    PrintRegions(regiondata, addRegion, item.DepartmentId);
                }
            }
            return webResponse.OK("请求成功", data);
        }
        // 递归打印区域数据
        public void PrintRegions(List<sys_department> sys_department, RegionDto region, Guid id)
        {
            var regiondata = sys_department.Where(c => c.ParentId == id);
            if (regiondata != null && regiondata.Count() > 0)
            {
                region.children = new List<RegionDto>();
                foreach (var item in regiondata)
                {
                    RegionDto addRegion = new RegionDto { value = item.DepartmentCode + "|" + item.DepartmentName, label = item.DepartmentName };
                    region.children.Add(addRegion);
                    PrintRegions(sys_department, addRegion, item.DepartmentId);
                }
            }
        }
    }

}
